<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller\Admin;

use Hyperf\HttpServer\Annotation\Controller;
use Core\Repository\Admin\UserRepository;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use App\Middleware\AdminAuthMiddleware;
use App\Middleware\LoginAuthMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * UserController
 * 首页控制器
 * @package App\Controller\Admin
 * @Controller(prefix="/user")
 * @Middlewares({
 *     @Middleware(LoginAuthMiddleware::class),
 *     @Middleware(AdminAuthMiddleware::class)
 * })
 * @property \Core\Repository\Admin\UserRepository $userRepo
 */
class UserController extends BaseController
{

    /**
     * index
     * 用户列表，用户管理
     * @return \Psr\Http\Message\ResponseInterface
     * @GetMapping(path="list")
     */
    public function index()
    {
        $reqParam = $this->request->all();
        $list = $this->userRepo->getUserList($reqParam);
        $data = [
            'total' => $list['total'],
            'items' => $list['data'],
        ];
        return $this->response->success($data);
    }

    /**
     * info()
     * @return \Psr\Http\Message\ResponseInterface
     * @GetMapping(path="info")
     */
    public function info() {
        $user = $this->request->getAttribute('user');
        $role = $this->request->getAttribute('user_role');
        $v = [
            "roles"=>$role,
            "introduction"=> $user['introduction'],
            "avatar"=>$user['avatar']?$user['avatar']:"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
            "name"=> $user['username']
        ];
        return    $this->response->success($v);
    }

    /**
     * getInfo
     * 根据id获取单条记录信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     * @GetMapping(path="get_info")
     */
    public function getInfo()
    {
        $reqParam = $this->request->all();
        $info = $this->userRepo->getInfo($reqParam['id']);
        $data = [
            'info' => $info,
        ];

        return $this->response->success($data);
    }

    /**
     * getRoles
     * 获取绑定角色信息
     * @return \Psr\Http\Message\ResponseInterface
     * @GetMapping(path="get_roles")
     */
    public function getRoles()
    {
        $data = $this->userRepo->getRolesList();

        return $this->response->success($data);
    }
    /**
     * store
     * 保存，新建、编辑都用该方法，区别是否有主键id
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Exception
     * @PostMapping(path="store")
     */
    public function store()
    {
        $reqParam = $this->request->all();
        $id = $this->userRepo->saveUser($reqParam);

        return $this->response->success($id);
    }
    /**
     * destroy
     * 删除用户
     * @return \Psr\Http\Message\ResponseInterface
     * @PostMapping(path="delete")
     */
    public function destroy()
    {
        $reqParam = $this->request->all();
        $this->userRepo->deleteInfo($reqParam['id']);

        return $this->response->success('ok');
    }
}
